//
//  TPUserSetViewController.m
//  Yimei
//
//  Created by xiaowu xiaowu on 2017/11/2.
//  Copyright © 2017年 xiaowu xiaowu. All rights reserved.
//

#import "TPUserSetViewController.h"
#import "WebViewController.h"
#import "TPUserInfoViewController.h"
#import "UITableView+TP.h"
#import "TPSetPasswordViewController.h"
#import "TPUpdateTelViewController.h"
@interface TPUserSetViewController ()
@property (nonatomic,copy)NSArray *titleArray;
@property (nonatomic,copy)NSArray *vcArray;
@end

@implementation TPUserSetViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    [self setup];
}

-(void)setup{
    
    
    
    self.titleArray = @[@[@"个人信息",[LoginModel shareLoginModel].isSetPsw ? @"修改密码" : @"设置密码",[LoginModel shareLoginModel].mobile.length == 0 ? @"绑定手机":@"修改手机",@"绑定微信"],@[@"清除缓存"]];
   
    self.tableViewStyle = UITableViewStyleGrouped;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 50.0, 0);
    [self.tableView reloadData];
  
    
    UIButton *outLoginButton = [UIButton buttonWithFrame:CGRectZero title:@"退出登录" font:fontSystemOfSize(17) color:[UIColor whiteColor]];
    [outLoginButton setBackgroundImageColor:TPAllBackgroundColor forState:UIControlStateNormal];
    [outLoginButton addTouchUpInsideTarget:self action:@selector(loginOut:)];
    [self.view addSubview:outLoginButton];
    [outLoginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0.0);
        make.bottom.mas_equalTo(0.0);
        make.height.mas_equalTo(50.0);
    }];
    
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.titleArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.titleArray[section] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifir = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifir];
    UISwitch *swit = [cell.contentView viewWithTag:99];
    if(!cell){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifir];
        
        
        cell.textLabel.font = fontSystemOfSize(16);
        cell.detailTextLabel.font = fontSystemOfSize(15);
        cell.detailTextLabel.textColor = TPBlack_999999;
        
        
        if(!swit)
         swit = [[UISwitch alloc]initWithFrame:CGRectMake(IPHONE_WIDTH - 70.0, 7.0, 70, 34.0)];
        swit.hidden = YES;
        swit.tag = 99;
        [swit addTarget:self action:@selector(switClick:) forControlEvents:UIControlEventValueChanged];
        swit.on = NO;
        [cell.contentView addSubview: swit];
        
    }
    
    cell.textLabel.textColor = TPBlack_333333;
    swit.hidden = YES;
    cell.detailTextLabel.text = [self getMemory];
    cell.detailTextLabel.hidden = YES;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = self.titleArray[indexPath.section][indexPath.row];
    switch (indexPath.row) {
        case 3:
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
            swit.hidden = NO;
        }
            break;
            
        default:
            break;
    }
    
    if(indexPath.section == 1){
        cell.detailTextLabel.hidden = NO;
//        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10.0f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
-(void)switClick:(UISwitch *)swit{
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
//    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    switch (indexPath.row) {
        case 0:
            {
                if(indexPath.section == 1){
                    [self getClear];
                    return;
                }
                TPUserInfoViewController *userInfoVC = [TPUserInfoViewController new];
                [self.navigationController pushViewController:userInfoVC animated:YES];
            }
            break;
        case 1:
        {
              TPSetPasswordViewController *userInfoVC = [TPSetPasswordViewController new];
            
              [self.navigationController pushViewController:userInfoVC animated:YES];
        }
            break;
        case 2:
        {
            TPUpdateTelViewController *userInfoVC = [TPUpdateTelViewController new];
            [self.navigationController pushViewController:userInfoVC animated:YES];
        }
            break;
            
        default:
            break;
    }
    
    
    
}
-(void)loginOut:(UIButton *)sender{
    [UIAlertView alertWithCallBackBlock:^(NSInteger buttonIndex) {
        
        if(buttonIndex == 0)
            return ;
        [[LoginModel shareLoginModel]clear];
        
        //        @weakify(self);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self leftBtnAction:nil];
        });
        
        //        [self presentViewController:[[UINavigationController alloc]initWithRootViewController:[TPTelLoginViewController new]] animated:YES completion:^{
        //            @strongify(self);
        //
        //        }];
        
        
    } title:@"是否退出？" message:nil cancelButtonName:@"取消" otherButtonTitles:@"确定", nil];
}


-(NSString *)getMemory{
    NSInteger m = [[SDImageCache sharedImageCache] getSize] + [[SDImageCache sharedImageCache]getDiskCount];
    CGFloat z = m/1024.0;
    return z > 1024 ? [NSString stringWithFormat:@"%.2f M",z/1024] : [NSString stringWithFormat:@"%.2f k",z];
}
- (void)getClear{
    
    
    [[SDImageCache sharedImageCache] clearMemory];
    [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
        [UIView xw_hideTitle:@"清除成功"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    }];
    
    NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"video"];
    
    
    NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
    NSNumber *sizeNumber = attributes[@"NSFileSize"];
    DLog(@"%@",[NSString stringWithFormat:@"%.1fM",sizeNumber.integerValue/1024.0]);
    
    [[NSFileManager defaultManager]createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil];
    
    NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager]enumeratorAtPath:path];
    
    for (NSString *name in enumerator)
    {
        
        NSString *PATH_MOVIE_FILE = [path stringByAppendingPathComponent:name];
        DLog(@"文件名--%@",name);
        [[NSFileManager defaultManager]removeItemAtPath:PATH_MOVIE_FILE error:nil];
        
        
    }
    
    
}


@end
